package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogain;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("users")
public class UserController {

    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String smsAppkey;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("list")
    public Object list(){
        List<UserInfo> list = userInfoService.list();
        return list;
    }

    @GetMapping("checkPhone")
    public Object checkPhone(String phone){
        boolean flag = userInfoService.checkPhone(phone);
        return !flag;
    }

    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){

        userInfoService.sendVerifyCode(phone,smsUrl,smsAppkey);
        //操作成功,

        return JsonResult.success();
    }

    @PostMapping("regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
        //接收参数,判空
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);

        return JsonResult.success();
    }

    @PostMapping("login")
    public Object login(String username,String password){
        //调用普通service,根据username与password查出user对象
        UserInfo user = userInfoService.login(username,password);
        //用redis的服务拿取token并将登陆对象加到redis中
        String token = userInfoRedisService.setToken(user);
        //将user和令牌存到浏览器中
        Map<String,Object> map=new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        JsonResult success = JsonResult.success();
        success.setData(map);
        return success;
    }


    @RequireLogain
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        return JsonResult.success();
    }

    //返回用户id的攻略id集合
    @RequireLogain
    @GetMapping("/strategies/favor")
    public Object favor(String userId) {
        //调用服务层方法
        List<String> stras = strategyStatisVORedisService.getUserStrategies(userId);
        return JsonResult.success(stras);
    }

    @RequireLogain
    @GetMapping("/get")
    public Object get(String id) {
        //调用服务层方法
        UserInfo userInfo = userInfoService.get(id);
        return JsonResult.success(userInfo);
    }
}
